import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class ImageTextItem extends StatefulWidget {
  ///图片地址
  final String url;

  ///文本内容
  final String? text;

  ///文本是否可以换行
  final bool isWrap;

  ///文本样式
  final TextStyle? style;

  ///文本与图片间距(为负值时文字覆盖于图片之上)
  final double? marginTop;

  ///容器背景
  final Color? boxColor;

  ///图片宽度
  final double width;

  ///图片高度
  final double height;

  ///点击事件
  final VoidCallback? onTap;

  ///组件直接间距
  final EdgeInsetsGeometry? margin;

  ///上图下文组件
  const ImageTextItem(
      {super.key,
      required this.url,
      required this.width,
      required this.height,
      this.text,
      this.style,
      this.marginTop,
      this.boxColor = Colors.white,
      this.onTap,
      this.isWrap = false,
      this.margin
      });

  @override
  State<StatefulWidget> createState() => _ImageTextItemState();
}

class _ImageTextItemState extends State<ImageTextItem> {
  @override
  Widget build(BuildContext context) {
    return BaseInteractive(
      childWidget: Container(
        width: _getBoxWidth().w,
        color: widget.boxColor,
        margin:widget.margin ,
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            BaseImage(
                url: widget.url, width: widget.width, height: widget.height),
            widget.text != null
                ? Container(
                    transform: Matrix4.translationValues(
                        0.0, widget.marginTop ?? 0, 0.0),
                    child: Container(
                      alignment: Alignment.center,
                      padding: EdgeInsets.only(bottom: 5.h),
                      child: Text(
                        widget.text!,
                        style: widget.style,
                      ),
                    ))
                : emptyWidget()
          ],
        ),
      ),
      onClick: () {
        widget.onTap!();
      },
    );
  }

  ///计算文本高度
  double _getTextHeight() {
    if (widget.text != null) {
      var initHeight = 6;

      ///字体高度
      var textHeight = widget.style?.fontSize ?? 12;
      return initHeight + textHeight;
    }
    return 0;
  }

  ///计算文本宽度
  double _getTextWidth() {
    if (widget.text != null) {
      var initFontSize = widget.style?.fontSize ?? 12;
      var initWidth = initFontSize + 3;
      return initWidth * widget.text!.length;
    }
    return 0;
  }

  ///计算盒子宽度
  double _getBoxWidth() {
    if (!widget.isWrap) {
      return _getTextWidth() > widget.width ? _getTextWidth() : widget.width;
    } else {
      return widget.width;
    }
  }
}
